package thread;

import java.io.*;
import java.util.Scanner;

public class Demo55 {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        System.out.println("请输入源文件路径：");
        String src = scanner.next();
        File srcFile = new File(src);
        if (!srcFile.isFile()) {
            System.out.println("源文件不是普通文件");
            return;
        }
        System.out.println("请输入目标文件路径：");
        String dest = scanner.next();
        File destFile = new File(dest);
        //不要求目标文件一定存在，但必须是在一个目录的下面
        if (!destFile.getParentFile().isDirectory()) {
            System.out.println("目标文件所在目录不存在");
            return;
        }
        //同时打开输入输出流
        try (InputStream inputStream = new FileInputStream(srcFile);
            OutputStream outputStream = new FileOutputStream(destFile)) {
            while (true) {
                byte[] bytes = new byte[1024];
                int n = inputStream.read(bytes);
                if (n == -1) {
                    break;
                }
                //从数组bytes中读了多少写多少
                outputStream.write(bytes, 0, n);
            }
        }
    }
}
